package com.ruoyi.common.utils;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ruoyi.common.Result;
import com.ruoyi.common.pojo.WxUser;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


/**
 * Web相关工具类
 */
public class WebUtil {
    private static GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static Gson gson = gsonBuilder.disableHtmlEscaping()
            .excludeFieldsWithModifiers(Modifier.PROTECTED, Modifier.TRANSIENT, Modifier.STATIC).create();

    /**
     * 发起http get请求
     */
    public static Result doGet(String requestUrl) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String responseContent = null;
        String result = null;

        try {
            HttpGet httpGet = new HttpGet(requestUrl);
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
            System.out.println("responseContent:" + responseContent);

            if (StringUtils.isEmpty(responseContent)) {
                return Result.FAIL();
            }
            if (responseContent.contains("errcode")) {
                WxUser userInfo = gson.fromJson(responseContent, WxUser.class);
                if (!"0".equals(userInfo.getErrcode()))
                    return Result.FAIL();
            }
            return Result.SUCCESS(responseContent);
        } catch (IOException e) {
            System.out.println("HTTP请求异常：" + e.getMessage());
            return Result.FAIL();
        }

    }

    private static String charset = "utf-8";
    private static HttpClient httpClient = HttpClients.createDefault();

    @SuppressWarnings({"unchecked", "rawtypes"})
    public static String doPost(String url, Map<String, String> map, Map<String, String> header) {
        HttpPost httpPost = null;
        String result = null;
        try {
            httpPost = new HttpPost(url);
            if (header != null) {
                for (String key : header.keySet()) {
                    httpPost.addHeader(key, header.get(key));
                }

            }
            // 设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<String, String> elem = (Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
            }
            if (list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    /**
     * 发送模板消息
     *
     * @param accessToken
     * @param data
     * @return 状态
     */
    public static String sendTemplateMsg(String sendTemplateMessageUrl, String accessToken, String data) {
        Map<String, String> header = null;
        //header.put("access_token", accessToken);

        String url = String.format("%s?access_token=%s", sendTemplateMessageUrl, accessToken);
        HttpPost httpPost = null;
        String result = null;
        try {
            httpPost = new HttpPost(url);
            if (header != null) {
                for (String key : header.keySet()) {
                    httpPost.addHeader(key, header.get(key));
                }

            }
            StringEntity stringEntity = new StringEntity(data, charset);
            httpPost.setEntity(stringEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}
